package validation.jexp;

import com.jgoodies.binding.adapter.BoundedRangeAdapter;
import com.jgoodies.binding.beans.PropertyConnector;
import com.jgoodies.binding.value.ConverterFactory;
import com.jgoodies.binding.value.ValueHolder;
import com.jgoodies.binding.value.ValueModel;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import java.text.DecimalFormat;

/**
 * JGoodies Binding: Bounded Range Adapter Example
 */
public class BoundedRangeAdapterExample extends JPanel {

    public BoundedRangeAdapterExample() {
        DefaultFormBuilder defaultFormBuilder = new DefaultFormBuilder(new FormLayout("p, 2dlu, p:grow"));
        defaultFormBuilder.setDefaultDialogBorder();
        ValueModel percentageModel = new ValueHolder();
        percentageModel.setValue(new Integer("1"));
        JSlider slider = new JSlider();
        BoundedRangeAdapter boundedRangeAdapter = new BoundedRangeAdapter(percentageModel, 0, 0, 100);
        slider.setModel(boundedRangeAdapter);
        defaultFormBuilder.append("Slider:", slider);

        JLabel sliderLabel = new JLabel();
        PropertyConnector propertyConnector = PropertyConnector.connect(
                ConverterFactory.createStringConverter(percentageModel, new DecimalFormat("#")),
                "value", sliderLabel, "text");
        propertyConnector.updateProperty2();
        defaultFormBuilder.append("Label:", sliderLabel);
        add(defaultFormBuilder.getPanel());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JGoodiesValidationUtils.showFrame("Bounded Range Adapter Example", new BoundedRangeAdapterExample());
        });
    }
}
